package com.cskaoyan.order.service.impl;

import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.order.constant.OrderRetCode;
import com.cskaoyan.order.converter.OrderConverter;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.entitys.OrderShipping;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dto.OrderDetailResponse;
import com.cskaoyan.order.dto.OrderItemDto;
import com.cskaoyan.order.dto.OrderListResponse;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.service.OrderQueryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @ClassName OrderQueryServiceImpl
 * @Description TODO
 * @Author XiaoWJ
 * @Date 2022/9/21 14:14
 **/
@Slf4j
@Service
public class OrderQueryServiceImpl implements OrderQueryService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Autowired
    OrderConverter orderConverter;


    @Override
    public OrderListResponse orderList(OrderListRequest request) {
        return null;
    }

    @Override
    public OrderDetailResponse orderDetail(String orderId) {

        // 查询订单信息
        Example example = new Example(Order.class);
        example.createCriteria().andEqualTo("orderId",orderId);

        List<Order> orders = orderMapper.selectByExample(example);

        if(CollectionUtils.isEmpty(orders)) {
            return null;
        }

        Order order = orders.get(0);

        Example example1 = new Example(OrderShipping.class);
        example1.createCriteria().andEqualTo("orderId",orderId);

        List<OrderShipping> orderShippings = orderShippingMapper.selectByExample(example1);

        if (CollectionUtils.isEmpty(orderShippings)) {
            log.error("订单：" + orderId + "的邮寄信息为空" );
            throw new BizException(OrderRetCode.SHIPPING_DB_QUERY_FAILED.getCode(),
                    OrderRetCode.SHIPPING_DB_QUERY_FAILED.getMessage());
        }
        // 查询订单对应的邮寄信息
        OrderShipping orderShipping = orderShippings.get(0);

        OrderDetailResponse orderDetailResponse = orderConverter.order2res(order, orderShipping);


        // 查询商品信息
        Example example2 = new Example(OrderItem.class);
        example2.createCriteria().andEqualTo("orderId",orderId);

        List<OrderItem> orderItems = orderItemMapper.selectByExample(example2);
        List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);

        orderDetailResponse.setGoodsList(orderItemDtos);

        return orderDetailResponse;
    }
}
